<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
// import moment from "monent"
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {
    SelectDataGoods: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataGoods()),
    SelectDataGoodsGroup: async () =>
      Vue.extend(
        await Vue.prototype.$vFN.Components.Base.SelectDataGoodsGroup()
      ),
    SelectDataGoodsClassify: async () =>
      Vue.extend(
        await Vue.prototype.$vFN.Components.Base.SelectDataGoodsClassify()
      ),
  },
  data() {
    return {
      appWindow: window,
      form: {
        // 活动名称
        activityName: "",
        // 活动状态 1未开始 2进行中 3已结束
        activityStatus: 1,
        // 活动类型 1满X元减 2满X元折
        activityType: 2,
        // 应用类型 1全部商品 2商品分组 3商品分类 4指定商品
        applyType: 1,
        // 	开始/结束时间
        startTimeStr: "",
        endTimeStr: "",
        // 下单可用抵扣
        deductOverlayLst: [],
        // 减免类型 1减一次 2每满一次减一次
        // reductionType: 1,

        // 适用商品
        fullDiscountProductLst: [],
        // 适用分组
        fullDiscountProductGroupsLst: [],
        // 适用分类
        fullDiscountProductCategoryLst: [],
        // 优惠内容
        fullDiscountItemLst: [
          {
            // 条件金额
            conditionValue: "",
            // 优惠金额
            discountValue: "",
          },
        ],
      },

      // 页面接口地址
      apiUrl: {
        create: "api/full-discount/createFullDiscount",
        update: "api/full-discount/updateFullDiscount",
        detail: "api/full-discount/getFullDiscount",
      },

      deducteList: "",

      isDisabled: false,
    };
  },
  computed: {},
  methods: {
    // form 提交添加/修改 的参数扩展方法
    mixinEdit_editFormSubmitExtend(data_) {
      if (data_.applyType == 2) {
        // if (this.$vFN.Utils.isEmpty(data_.fullDiscountProductGroupsLst)){
        //   this.$vFN.Tools.messageError
        //   return
        // }
        data_.fullDiscountProductGroupsLst = [
          {
            groupsId: data_.fullDiscountProductGroupsLst[0],
            subGroupsId: data_.fullDiscountProductGroupsLst[1],
          },
        ];

        // 分类请空
        data_.fullDiscountProductCategoryLst = [];
      } else if (data_.applyType == 3) {
        data_.fullDiscountProductCategoryLst = [
          {
            categoryId: data_.fullDiscountProductCategoryLst[0],
            subCategoryId: data_.fullDiscountProductCategoryLst[1],
          },
        ];

        // 分组请空
        data_.fullDiscountProductGroupsLst = [];
      } else if (data_.applyType == 4) {
        let _list = [];
        data_.fullDiscountProductLst.forEach((item) => {
          _list.push({
            productId: item,
          });
        });
        data_.fullDiscountProductLst = _list;

        // 分组和分类请空
        data_.fullDiscountProductCategoryLst = [];
        data_.fullDiscountProductGroupsLst = [];
      }

      return data_;
    },

    // 添加活动层级
    addfullDiscountItemLst() {
      let _list = {
        // 条件金额
        conditionValue: "",
        // 优惠金额
        discountValue: "",
      };
      this.form.fullDiscountItemLst.push(_list);

      if (this.form.fullDiscountItemLst.length >= 10) {
        this.form.fullDiscountItemLst.length = 10;
      }
    },
    // 删除活动层级
    delfullDiscountItemLst(item, index) {
      this.$delete(this.form.fullDiscountItemLst, index);
    },

    // 编辑修改的参数扩展方法
    mixinEdit_editDataExtend(data_) {
      // console.log(1111,data_)
      console.log("type", this.mixinEdit_editType);

      // 添加
      if (this.mixinEdit_editType == "add") {
        this.isDisabled = false;
      }
      // 修改
      else if (this.mixinEdit_editType == "update") {
        if (data_.activityStatus === 2) {
          this.isDisabled = true;
        }
      }
      // 复制
      else if (this.mixinEdit_editType == "copy") {
        this.isDisabled = false;
      }

      if (
        this.mixinEdit_editType == "update" ||
        this.mixinEdit_editType == "copy"
      ) {
        // 处理返回的抵扣资产id的数组
        this.disposeIntegralType(data_);
        // 处理返回的优惠券对应商品内容
        if (!this.$vFN.Utils.isEmpty(data_.fullDiscountProductGroupsLst)) {
          let newArr = [];
          newArr.push(data_.fullDiscountProductGroupsLst[0].groupsId);

          console.error("woc");
          if (data_.fullDiscountProductGroupsLst[0].subGroupsId) {
            newArr.push(data_.fullDiscountProductGroupsLst[0].subGroupsId);
          }

          data_.fullDiscountProductGroupsLst = newArr;
        }
        if (!this.$vFN.Utils.isEmpty(data_.fullDiscountProductCategoryLst)) {
          let newArr = [];
          newArr.push(data_.fullDiscountProductCategoryLst[0].categoryId);

          if (data_.fullDiscountProductCategoryLst[0].subCategoryId) {
            newArr.push(data_.fullDiscountProductCategoryLst[0].subCategoryId);
          }

          data_.fullDiscountProductCategoryLst = newArr;
        }
        if (!this.$vFN.Utils.isEmpty(data_.fullDiscountProductLst)) {
          data_.fullDiscountProductLst = data_.fullDiscountProductLst.map(
            (item) => {
              return item.productId;
            }
          );
        }
      }

      return data_;
    },

    async getIntegralType() {
      this.mixinEdit_loading = true;
      let res = await this.$vFN.Http.post(
        {
          url: "api/full-discount/getIntegralType",
        },
        window
      );
      this.mixinEdit_loading = false;
      if (res._ok) {
        this.deducteList = res.data.map((item) => {
          return {
            ...item,
            isChecked: false,
          };
        });
        console.log("deducteList", this.deducteList);
      }
    },

    // 处理返回的资产id数组  判断是否选中
    disposeIntegralType(data_) {
      this.form.deductOverlayLst = data_.deductOverlayLst;
      // console.log("this.deducteList", this.deducteList);
      // console.log("this.form.deductOverlayLst", this.form.deductOverlayLst);

      this.deducteList.forEach((item) => {
        item.id = String(item.id);
        let findIndex = this.form.deductOverlayLst.indexOf(item.id);
        // console.log("index", index);
        if (findIndex > -1) {
          item.isChecked = true;
        }
      });

      // console.log("this.deducteList", this.deducteList);
    },

    // 根据层级不同，返回不同的最小值
    minConditionValue(index_) {
      // console.log("index_", index_);
      let num = 0;
      if (index_ > 0) {
        num = this.form.fullDiscountItemLst[index_ - 1].conditionValue + 1;
      } else {
        num = 1;
      }
      // console.log("num", num);
      return num;
    },

    radioChange(label_) {
      console.log("label_", label_);
      if (label_ == 2 || label_ == 4) {
        this.form.fullDiscountItemLst.forEach((item) => {
          item.discountValue = 10;
        });
      }
    },

    // 初始化
    async init() {
      await this.getIntegralType();
      // 初始化数据
      this.mixinEdit_editBoxInit({
        model: "form",
        id: this.id,
      });
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>